package com.yunzhou.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.common.util.string.StringUtil;
import weaver.conn.RecordSet;

import java.util.HashMap;

public class DataChangeServiceImpl implements DataChangeService {

    Log log = LogFactory.getLog(DataChangeServiceImpl.class);

    public String getSingleBrowseValue(String value1, String TableName, String Condition, String Value2) {
        String result = "";
        String sql = "select " + value1 + " from " + TableName + " where " + Condition + " = '" + Value2 + "'";
        log.info("getSingleBrowseValue---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            result = rs.getString(value1);
        }
        log.info("getSingleBrowseValue---value:" + result);
        return result;
    }

    public String getMultipleBrowseValue(String value1, String TableName, String Condition, String Value2, HashMap map) {
        String result1 = "";
        String sql = "select " + value1 + " from " + TableName + " where " + Condition + " in (" + Value2 + ")";
        log.info("getMultipleBrowseValue---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            result1 = rs.getString(value1);
        }
        String result2 = "";
        if (StringUtil.isNotNullAndEmpty(result1) && result1 != "null") {
            String[] as = result1.split(",");
            for (int k = 0; k < as.length; k++) {
                if (result2.equals("")) {
                    result2 = map.get(as[k]).toString();
                } else {
                    result2 += "," + map.get(as[k]);
                }
            }
        }
        log.info("getMultipleBrowseValue---value:" + result2);
        return result2;
    }

    public HashMap getSpecifiedTableMap(String TableName, String id, String value) {
        HashMap map = new HashMap();
        String sql = "select * from " + TableName;
        log.info("getSpecifiedTableMapValue---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        while (rs.next()) {
            map.put(rs.getString(id), rs.getString(value));
        }
        return map;
    }

    public Integer getDroplistByName(String WorkFlowName, String Condition, String Value) {
        Integer selectValue=-1;
        String sql ="select distinct(t1.fieldid),(select fieldname from workflow_billfield where id = t1.fieldid) ms,\n" +
                "t1.id,t1.selectvalue,t1.selectname,t4.workflowname\n" +
                "from workflow_selectitem t1,workflow_nodeform t2,workflow_flownode t3,workflow_base t4\n" +
                "where t1.fieldid = t2.fieldid and t2.nodeid = t3.nodeid and t3.workflowid = t4.id\n" +
                "and t4.workflowname = '"+WorkFlowName+"'\n" +
                "and (select fieldname from workflow_billfield where id = t1.fieldid) = '"+Condition+"'\n" +
                "and t1.selectname='"+Value+"'\n" +
                "order by t1.fieldid ";

        log.info("getDroplistByName---sql:"+sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()){
            selectValue=rs.getInt("selectvalue");
        }
        log.info("getDroplistByName---selectValue:"+selectValue);
        return selectValue;
    }

    public String getSelectNameById(String fieldid,String selectvalue) {
        String result = "";
        String sql = "select selectname from workflow_selectitem where fieldid ='" + fieldid + "' and  selectvalue = '" + selectvalue + "'";
        log.info("getSelectNameById---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            result = rs.getString("selectname");
        }
        log.info("getSingleBrowseValue---value:" + result);
        return result;
    }

    public String getActiveVersionID(String workflowid) {
        String activeVersionID = "";
        String sql = "select activeVersionID from workflow_base where id = " + workflowid;
        log.info("getActiveVersionID---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            activeVersionID = rs.getString("activeVersionID");
        }
        if (StringUtil.isNullOrEmpty(activeVersionID) || activeVersionID == "null" ) {
            activeVersionID = workflowid;
        }
        log.info("getActiveVersionID---activeVersionID:" + activeVersionID);
        return activeVersionID;
    }

    public String getPrimevalID(String workflowid) {
        String primevalID = "";
        String activeVersionID = "";
        String sql = "select activeVersionID from workflow_base where id = " + workflowid;
        log.info("getPrimevalID---sql1:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            activeVersionID = rs.getString("activeVersionID");
        }
        if (StringUtil.isNullOrEmpty(activeVersionID) || activeVersionID == "null") {//无活动版本
            primevalID = workflowid;
        } else{//有活动版本
            sql = "select MIN(id) as id from workflow_base where activeVersionID = " + activeVersionID;
            log.info("getPrimevalID---sql2:" + sql);
            rs.execute(sql);
            if (rs.next()) {
                primevalID = rs.getString("id");
            }
        }
        log.info("getPrimevalID---primevalID:" + primevalID);
        return primevalID;
    }

    public String getCurrentoperatorFromNodeName(String requestid, String nodeName) {
        String Currentoperator = "";
        String sql = "select top 1 (select nodename from workflow_nodebase where id = nodeid) as jdmc,(select lastname from hrmresource where id = userid) as xm,*\n" +
                " from workflow_currentoperator where (select nodename from workflow_nodebase where id = nodeid) like '%" + nodeName + "%' and requestid = " + requestid +
                " and preisremark = 0 and isremark = 2 order by receivedate desc ,receivetime desc";
        log.info("getActiveVersionID---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            Currentoperator = rs.getString("userid");
        }
        log.info("getCurrentoperatorFromNodeName---Currentoperator:" + Currentoperator);
        return Currentoperator;
    }

}
